package cn.cdm.controller;

import cn.cdm.domain.MiaoshaUser;
import cn.cdm.domain.User;
import cn.cdm.redis.RedisService;
import cn.cdm.redis.UserKey;
import cn.cdm.result.CodeMsg;
import cn.cdm.result.Result;
import cn.cdm.service.MiaoshaUserService;
import cn.cdm.service.UserService;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@Controller
@RequestMapping("/demo")
public class SampleController {

	@Autowired
    UserService userService;
	
	@Autowired
	RedisService redisService;

	@Autowired
    MiaoshaUserService miaoshaUserService;


     /**
     * Themaleaf模板与Spring整合，返回hello.html页面
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String themaleafDemo(Model model) {
        model.addAttribute("name", "Joshua");
        return "hello";
    }



    @RequestMapping("/user")
    @ResponseBody
    public Result<String> user(HttpServletResponse response, @RequestParam("token")String token) {
        System.out.println(token);
        MiaoshaUser byToken = miaoshaUserService.getByToken(response, token);
        System.out.println(byToken);
        return Result.success("Hello，user" + byToken);
    }

	
    @RequestMapping("/hello")
    @ResponseBody
    public Result<String> home() {
        return Result.success("Hello，world");
    }
    
    @RequestMapping("/error")
    @ResponseBody
    public Result<String> error() {
        return Result.error(CodeMsg.SESSION_ERROR);
    }
    
    @RequestMapping("/hello/themaleaf")
    public String themaleaf(Model model) {
        model.addAttribute("name", "Joshua");
        return "hello";
    }
    
    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet() {
    	User user = userService.getById(1);
        return Result.success(user);
    }
    
    
    @RequestMapping("/db/tx")
    @ResponseBody
    public Result<Boolean> dbTx() {
    	/*userService.transactionalTest();
        return Result.success(true);*/

        // 处理异常的写法：在Controller层捕获异常，然后将错误信息封装成结果集，返回给前端
        /*try {
            userService.transactionalTest();
        } catch (Exception e) {
            log.error("打印：插入用户表失败");
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(true);*/

        // 使用Web层的全局异常处理器
        boolean insertSuccess = userService.transactionalTest();
        if (insertSuccess) {
            return Result.success(true);
        } else {
            log.error("打印插入失败888");
            return Result.error(CodeMsg.SERVER_ERROR);
        }

    }
    
    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<User> redisGet() {
    	User  user  = redisService.get(UserKey.getById, ""+1, User.class);
        return Result.success(user);
    }
    
    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<Boolean> redisSet() {
    	User user  = new User();
    	user.setId(1);
    	user.setName("1111");
    	// 最终的key为：UserKey:id1
    	redisService.set(UserKey.getById, ""+1, user);//UserKey:id1
        return Result.success(true);
    }
    
    
}
